import 'dart:io';

import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:image_editor/image_editor.dart';
import 'package:matisse_android_plugin/clip/custom_image_clip_page.dart';
import 'package:matisse_android_plugin/src/ios/screen_util.dart';

///裁剪
class CustomImageClipState extends State<CustomImageClipPage> {
  GlobalKey<ExtendedImageEditorState> editorKey =
  GlobalKey<ExtendedImageEditorState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: widget?.backgroundColor ?? Colors.black,
      body: Container(
        child: Stack(
          children: <Widget>[
            ExtendedImage.file(
              widget?.file ??
                  File(
                      "/Users/zengxiangxi/Library/Developer/CoreSimulator/Devices/154B9D63-D5BD-44E4-99A5-43F74048676E/data/Containers/Data/Application/9BBFBCBA-100F-49C7-9365-3AAC2F14C598/tmp//1585122195557"),
              fit: BoxFit.contain,
              extendedImageEditorKey: editorKey,
              mode: ExtendedImageMode.editor,
              initEditorConfigHandler: (state) {
                return widget?.editorConfig ??
                    EditorConfig(
                        maxScale: 8.0,
                        cropRectPadding:
                        EdgeInsets.all(widget?.marginValue ?? 30),
                        hitTestSize: 20.0,
                        lineColor: widget?.lineColor ?? Colors.white,
                        cornerSize: Size(0, 0),
                        lineHeight: widget?.lineWidth ?? 2,
                        cropAspectRatio: widget?.aspectRatio,
                        editorMaskColorHandler: (context, bo) {
                          return widget?.maskColor ?? Color(0x80000000);
                        }
                    );
              },
            ),
            Positioned(
              bottom: ScreenMatisseUtil.getBottomBarH(context),
              left: 0,
              child: GestureDetector(
                behavior: HitTestBehavior.translucent,
                onTap: () => Navigator.of(context).pop(),
                child: Container(
                  padding: const EdgeInsets.all(20),
                  child: widget?.leftCancelWidget ??
                      Text(
                        "取消",
                        style: TextStyle(color: Colors.white, fontSize: 19),
                      ),
                ),
              ),
            ),
            Positioned(
              bottom: ScreenMatisseUtil.getBottomBarH(context),
              right: 0,
              child: GestureDetector(
                behavior: HitTestBehavior.translucent,
                onTap: () {
                  managerClipData();
                },
                child: Container(
                  padding: const EdgeInsets.all(20),
                  child: widget?.rightClipWidget ??
                      Text(
                        "裁剪",
                        style: TextStyle(color: Colors.white, fontSize: 19),
                      ),
                ),
              ),
            ),
            Positioned(
              top: ScreenMatisseUtil.getStatusBarH(context),
              left: 0,
              child: GestureDetector(
                behavior: HitTestBehavior.translucent,
                onTap: () {
                  Navigator.of(context).pop("");
                },
                child: Container(
                  child: widget?.topLeftWidget ??
                      Container(),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  ///处理裁剪数据
  void managerClipData() async {
    final ExtendedImageEditorState state = editorKey?.currentState;
    if (state == null) {
      print("裁剪失败");
      return;
    }
    final EditActionDetails editAction = state.editAction;

    final Rect cropRect = state.getCropRect();
    final img = state.rawImageData;
    final rotateAngle = editAction.rotateAngle.toInt();
    final flipHorizontal = editAction.flipY;
    final flipVertical = editAction.flipX;
    ImageEditorOption option = ImageEditorOption();

    if (editAction.needCrop) {
      option.addOption(ClipOption.fromRect(cropRect));
    }

    if (editAction.needFlip) {
      option.addOption(
          FlipOption(horizontal: flipHorizontal, vertical: flipVertical));
    }

    if (editAction.hasRotateAngle) {
      option.addOption(RotateOption(rotateAngle));
    }
    final result = await ImageEditor.editImageAndGetFile(
      image: img,
      imageEditorOption: option,
    );
    print("zxx:-----裁剪图片路径：${result?.path}");
    Navigator.of(context).pop(result?.path);
  }

  @override
  void dispose() {
    editorKey = null;
    super.dispose();
  }
}
